;
/*
var treeMenuJason = [ {
	"id" : 1,
	"name" : "HOME",
	"pId" : 0,
	"url" : "home"
}, {
	"id" : 2,
	"name" : "CONTENT",
	"pId" : 0,
	"url" : ""
}, {
	"id" : 3,
	"name" : "PAGING",
	"pId" : 2,
	"url" : "test_overview"
}, {
	"id" : 4,
	"name" : "NEWS",
	"pId" : 2,
	"url" : "news_overview"
}, {
	"id" : 5,
	"name" : "MENU1",
	"pId" : 0,
	"url" : "news_overview"
}, {
	"id" : 6,
	"name" : "MENU1.1",
	"pId" : 5,
	"url" : "news_overview"
}, {
	"id" : 7,
	"name" : "MENU1.2",
	"pId" : 5,
	"url" : "news_overview"
}, {
	"id" : 8,
	"name" : "MENU1.1.1",
	"pId" : 7,
	"url" : "news_overview"
}, {
	"id" : 9,
	"name" : "Menu Manager",
	"pId" : 0,
	"url" : "tree_menu_overview"
} ];
*/
var settings = {
		addDiyDom : addDiyDom,
		isSimpleData : true,              //数据是否采用简单 Array 格式，默认false  
	    treeNodeKey : "id",               //在isSimpleData格式下，当前节点id属性  
	    treeNodeParentKey : "pId",        //在isSimpleData格式下，当前节点的父节点id属性  
	    showLine : true                  //是否显示节点间的连线  
	   // checkable : true                  //每个节点上是否显示 CheckBox
};
var MENU_TREE_PREFIX = "tree_menu_tree_";
var MENU_TREE_EDIT_ICON_SUBFIX = "_editMenuIcon";
var MENU_TREE_ADD_ICON_SUBFIX = "_addMenuIcon";
var MENU_TREE_DEL_ICON_SUBFIX = "_delMenuIcon";
var MENU_TREE_EDIT_DIV_SUBFIX = "_editMenuDiv";
var MENU_TREE_ADD_DIV_SUBFIX = "_addMenuDiv";
var MENU_TREE_URL = "_a";
var MENU_TREE_NAME = "_span";
function addDiyDom(treeId, treeNode) {
	// nodeId = tree_menu_tree_1
	var nodeId = treeNode.tId;
	var menuId = treeNode.id;
	var isParent = treeNode.isParent;
	var aObj = $("#" + nodeId + MENU_TREE_URL);
    aObj.after(
    			Lemon.MenuOperator.generateEditIcon(nodeId, menuId) + 
    			Lemon.MenuOperator.generateAddIcon(nodeId, menuId) +
    			Lemon.MenuOperator.generateDelIcon(nodeId, menuId, isParent) +
    			Lemon.MenuOperator.generateEditDivHtml(nodeId, menuId, isParent) + 
    			Lemon.MenuOperator.generateAddDivHtml(nodeId, menuId, treeNode.name)
    		);
    Lemon.MenuOperator.bindEventForEdit(nodeId, menuId);
    Lemon.MenuOperator.bindEventForAdd(nodeId, menuId);
    Lemon.MenuOperator.bindEventForDel(nodeId, menuId);
};

Lemon.MenuOperator = {
		portletId : '', 
		actionUrl : '',
		setting : '',
		setUp : function(portletId, setting, actionUrl, expandAll){
			// init the params
			Lemon.MenuOperator.portletId = portletId;
			Lemon.MenuOperator.actionUrl = actionUrl;
			Lemon.MenuOperator.setting = setting;
			
			$.ajax({type : 'POST',dataType : "html",url : actionUrl,
				error : function() {alert('请求失败');},
				success : function(data) { 
					var treeNodes = eval(data); 
					var $portletTree = $("#" + portletId + "_tree");
					if(setting == null || setting == 'undefined')
						setting = settings;
					
					var zTree = $portletTree.zTree(setting, treeNodes);
					if(expandAll == false || expandAll == true)
						zTree.expandAll(expandAll);
					/*
					if(defaultSelectNode != null && defaultSelectNode != undefined && defaultSelectNode != 'undefined'){
						// when add a new menu, refresh the menu, and set the update node expand.
						var defaultExpandNode = '';
						var nodes = zTree.getNodes();
						for(var i=0; i < nodes.length; i++){
							if((MENU_TREE_PREFIX + nodes[i].id)  == defaultSelectNode)
								defaultExpandNode = nodes[i];
						}
						zTree.expandNode(defaultExpandNode, true);
						//zTree.expandAll(true);
					}*/
				}
			});
		},
		bindEventForEdit : function(nodeId, menuId){
			//pop up modal for edit
			$('#'+ nodeId + MENU_TREE_EDIT_ICON_SUBFIX ).click(
					function(){
						// pop the update menu modal
						$editDiv = $('#'+ nodeId + MENU_TREE_EDIT_DIV_SUBFIX ).clone();
						$editDiv.dialog({
							title:"Edit the Menu",
							resizable: false,
							//height:300,
							modal: true,
							buttons: {
								"Update": function() {
									// update the menu
									Lemon.MenuOperator.updateMenuInfo($editDiv, nodeId, menuId);
									$( this ).dialog( "close" );
								},
								Cancel: function() {
									$( this ).dialog( "close" );
								}
							}
						});
						// get detail data for menu, and set the data.
						$.ajax({type : 'POST',	dataType : "json",url : 'goal/menu!findMenuById.action',data: 'menu.id='+menuId,
							error : function() {alert('请求失败');},
							success : function(data) { 
								if(data.state== 'S'){
									// update the edit div value
									$editDiv.find('.menuName').val(data.data.name);
									$editDiv.find('.menuUrl').val(data.data.url);
									
								}else{
									alert("Update Menu Error! Please try again!");
								}
							}
						});
						
					});
		},
		bindEventForAdd : function(nodeId, parentId){
			//tree_menu_tree_9_addMenuIcon
			$('#'+ nodeId + MENU_TREE_ADD_ICON_SUBFIX ).click(
					function(){
						// pop the update menu modal
						$addDiv = $('#'+ nodeId + MENU_TREE_ADD_DIV_SUBFIX ).clone();
						$addDiv.dialog({
							title:"Add New Menu",
							resizable: false,
							//height:300,
							modal: true,
							buttons: {
								"Add": function() {
									// update the menu
									var flag = Lemon.MenuOperator.addNewMenu($addDiv, nodeId, parentId);
									if(!flag)
										$( this ).dialog( "close" );
								},
								Cancel: function() {
									$( this ).dialog( "close" );
								}
							}
						});
					});
		},
		bindEventForDel : function(nodeId, menuId){
			
			$('#'+ nodeId + MENU_TREE_DEL_ICON_SUBFIX ).click(function(){
				if(confirm('Are you sure to delete the menu?')){
					// get detail data for menu, and set the data.
					$.ajax({type : 'POST',	dataType : "json",url : 'goal/menu!deleteMenuById.action',data: 'menu.id='+menuId,
						error : function() {alert('请求失败');},
						success : function(data) { 
							if(data.state== 'S'){
								// reload the ztree
								Lemon.MenuOperator.refreshZTree();
							}else if((data.state== 'T')){
								alert("Sorry, You don't have the permission to operate it!");
							}else{
								alert("Update Menu Error! Please try again!");
							}
						}
					});
				}
			});
			
		},
		updateMenuInfo : function($editDiv, nodeId, menuId){
			 //saveId = tree_menu_tree_8_saveMenu id=8
			 var menuName = $editDiv.find('.menuName').val();
			 var menuUrl = $editDiv.find('.menuUrl').val();
			 var params = "menu.id=" + menuId + "&menu.name=" + menuName + "&menu.url=" + menuUrl;
			 //save the menu info
			 $.ajax({type : 'POST',	dataType : "json",url : 'goal/menu!updateMenuItem.action',data: params,
					error : function() {alert('请求失败');},
					success : function(data) { 
						if(data['state']== 'S'){
							Lemon.MenuOperator.refreshZTree();
						}else if((data.state== 'T')){
							alert("Sorry, You don't have the permission to operate it!");
						}else{
							alert("Update Menu Error! Please try again!");
						}
					}
				});
		},
		addNewMenu : function($addDiv, nodeId){
			 //saveId = tree_menu_tree_8_saveMenu id=8
			 var menuName = $addDiv.find('.menuName').val();
			 if(menuName == ''){
				 Lemon.MenuOperator.validMessage($addDiv, 'Menu name');
				 return false;
			 }
			 var menuUrl = $addDiv.find('.menuUrl').val();
			 if(menuUrl == ''){
				 Lemon.MenuOperator.validMessage($addDiv, 'Menu url');
				 return false;
			 }
			 var menuParentId = $addDiv.find('.menuParentId').val();
			 var params = "menu.name=" + menuName  + "&parentid=" + menuParentId + "&menu.url=" + menuUrl ;
			 //save the menu info 
			 $.ajax({type : 'POST',	dataType : "json",url : 'goal/menu!addMenuItem.action',data: params,
					error : function() {alert('请求失败');},
					success : function(data) { 
						if(data['state']== 'S'){
							Lemon.MenuOperator.refreshZTree();
						}else if((data.state== 'T')){
							alert("Sorry, You don't have the permission to operate it!");
						}else{
							alert("Add Menu Error! Please try again!");
						}
					}
				});
		},
		generateEditIcon : function(nodeId, menuId){
			var editIcon = '';
			//class = tree_menu_tree_9_editMenuIcon
			editIcon = "<img title='Edit The Menu' id='" + nodeId + MENU_TREE_EDIT_ICON_SUBFIX  +"' menuId=" + menuId +" src=css/zTreeStyle/img/edit.png style='cursor:pointer'/>";
			return editIcon;
		},
		generateAddIcon : function(nodeId, menuId){
			var editIcon = '';
			//class = tree_menu_tree_9_editMenuIcon
			editIcon = "&nbsp;&nbsp;<img title='Add New Menu' id='" + nodeId + MENU_TREE_ADD_ICON_SUBFIX  +"' menuId=" + menuId +" src=css/zTreeStyle/img/add.png style='cursor:pointer'/>";
			return editIcon;
		},
		generateDelIcon : function(nodeId, menuId, isParent){
			if(isParent)return '';
			var editIcon = '';
			//class = tree_menu_tree_9_editMenuIcon
			editIcon = "&nbsp;&nbsp;<img title='Delete The Menu' id='" + nodeId + MENU_TREE_DEL_ICON_SUBFIX  +"' menuId=" + menuId +" src=css/zTreeStyle/img/delete.png style='cursor:pointer'/>";
			return editIcon;
		},
		generateEditDivHtml :  function(nodeId, menuId, isParent){
			//id = tree_menu_tree_9_editMenuDiv
			var editMenuDiv = '';
			editMenuDiv += '<div id="' + nodeId + MENU_TREE_EDIT_DIV_SUBFIX   + '"style=overflow:hidden; class="hidden" menuId='+ menuId +'>';
			editMenuDiv += 'Name:<input class="menuName" style="height:18px;" type="text" value="" /><br/><br/>';
			// for menu with link, if the node have child, disable the url
			if(!isParent){
			//if(url != '' && url != undefined && url != 'undefined'){
				editMenuDiv += ' &nbsp;URL:<input class="menuUrl" style="height:18px;" type="text" value="" /><br/><br/>';
			}else{
				// for menu is folder type without the url
				editMenuDiv += ' &nbsp;URL:<input class="menuUrl" style="height:18px;" disabled="true" type="text" value="" /><br/>';
			}
			editMenuDiv += '</div>';
			return editMenuDiv;
		},
		generateAddDivHtml :  function(nodeId, parentId, parentName){
			//id = tree_menu_tree_9_editMenuDiv
			var editMenuDiv = '';
			editMenuDiv += '<div id="' + nodeId + MENU_TREE_ADD_DIV_SUBFIX   + '"style=overflow:hidden; class="hidden" parentId='+ parentId +'>';
			editMenuDiv += '	Name:<input class="menuName" style="height:18px;" type="text" value="" /><br/><br/>';
			editMenuDiv += '	&nbsp;URL:<input class="menuUrl" style="height:18px;" type="text" value="" /><br/><br/>';
			editMenuDiv += '	&nbsp;Parent Name : ';
			editMenuDiv += '	<select name="menuParentId" class="menuParentId">';
			editMenuDiv += '		<option value="'+ parentId +'">'+ parentName +'</option>';
			editMenuDiv += '		<option value="" }>No Parent</option>';
			editMenuDiv += '	</select><br/>';
			editMenuDiv += '	<div class="validMessage" ></div><br/>';
			editMenuDiv += '</div>';
			return editMenuDiv;
		},
		validMessage : function($div, property){
			$div.find('.validMessage').text('');
			$div.find('.validMessage').text(property + ', can not be null!');
				
		},
		refreshZTree : function(){
			//$('#'+ Lemon.MenuOperator.portletId + '_tree').empty();
			Lemon.MenuOperator.setUp(
					Lemon.MenuOperator.portletId,
					Lemon.MenuOperator.setting,
					Lemon.MenuOperator.actionUrl, true
			);
		}

};

